<template>
  <div @click="$router.push('/article/'+artId)">
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{article.title}}</span>
          <!-- 单张图片 -->
          <img v-lazy="article.cover.images[0]" alt="" class="thumb" v-if='article.cover.type===1'>
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="article.cover.type===3">
          <img v-for="(item,index) in article.cover.images" :key="index" v-lazy="item" alt="" class="thumb">
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <span>{{article.aut_name}} &nbsp;&nbsp; {{article.comm_count}}评论 &nbsp;&nbsp; {{article.pubdate|dataFormat}}</span>
          <!-- 关闭按钮 -->
          <van-icon name="cross" v-if="closeable" @click.stop="show=true"/>
        </div>
      </template>
    </van-cell>
    <van-action-sheet v-model="show" cancel-text="取消" :closeable='false' @closed='isFirst=true' get-container="body">
      <!-- 一级面板 -->
      <div v-if="isFirst">
     <van-cell :title="item.name" clickable class="center-title" v-for="item in actions" :key="item.name" @click="onCellClick(item.name)"/>
      </div>
      <!-- 二级面板 -->
      <div v-else>
       <van-cell title="返回" clickable class="center-title" @click="isFirst=true"></van-cell>
       <van-cell :title='item.label' clickable class="center-title" v-for="item in reports" :key="item.type" @click="reportArtcle(item.type)"></van-cell>

      </div>
    </van-action-sheet>
  </div>
</template>

<script>
import reports from '@/api/reports.js'
import { dislikeArtticleAPI, reportArtticleAPI } from '@/api/homeAPI.js'// 不喜欢文章接口
export default {

  name: 'ArtItem',
  props: {
    article: {
      type: Object,
      required: true
    },
    closeable: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    artId () {
      return this.article.art_id.toString()
    }
  },
  data () {
    return {
      show: false,
      actions: [
        { name: '不感兴趣' },
        { name: '反馈垃圾内容' },
        { name: '拉黑作者' }
      ],
      isFirst: true,
      reports
    }
  },
  methods: {
    async onCellClick (name) {
      if (name === '不感兴趣') {
        const { data: res } = await dislikeArtticleAPI(this.arstId)
        if (res.message === 'OK') {
          this.$emit('remove-article', this.artId)
        }
        this.show = false
      } else if (name === '拉黑作者') {
        console.log('拉黑作者')
        this.show = false
      } else if (name === '反馈垃圾内容') {
        this.isFirst = false
      }
    },
    async reportArtcle (type) {
      try {
        await reportArtticleAPI(this.artId, type)
      } catch {
        this.$toast.fail('已举报过该文章')
      }
      this.$emit('remove-article', this.artId)
      this.show = false
    }
  }
}
</script>

<style lang="less" scoped>
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.thumb {
  // 矩形黄金比例：0.618
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.thumb-box {
  display: flex;
  justify-content: space-between;
}
.center-title {
  text-align: center;
}
</style>
